package cn.ainit.sys.menu;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.map.ListOrderedMap;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import cn.ainit.common.ReturnObject;
import cn.ainit.sys.dz.entity.SysDzSource;

@Service("menuManagerBase")
public class MenuManagerImpl implements MenuManager {

	private MenuDao menuDao;

	@Resource
	public void setMenuDao(MenuDao menuDao) {
		this.menuDao = menuDao;
	}

	@Override
	public List<SysFunctions> getAllMenu() throws Exception {
		return menuDao.getAllMenu();
	}

	@Override
	public List<SysFunctions> getMenuById(String funcId) throws Exception {
		return menuDao.getMenuById(funcId);
	}

	@Override
	public List<SysFunctions> getListSup(String superId) throws Exception {
		return this.menuDao.getListByFId(superId);
	}

	@Override
	public List<SysDzSource> getAllDzSource() throws Exception {
		return menuDao.getAllDzSource();
	}

	@Override
	public List<SysDzSource> getDzSourceByDBI(String dbi) throws Exception{
		return menuDao.getDzSourceByDBI(dbi);
	}

	@Override
	public List<SysDzSource> getDzSourceByDBIAndShowpk(String dbi, String showpk) throws Exception{
		return menuDao.getDzSourceByDBIAndShowpk(dbi, showpk);
	}

	@Override
	public List<SysDzSource> getDzSourceByDBIAndShowpkNot(String dbi, String showpk) throws Exception{
		return menuDao.getDzSourceByDBIAndShowpkNot(dbi, showpk);
	}

	@Override
	public List<SysDzSource> getDzSourceByDBIAndShowpkNotIn(String dbi, String showpkNotIn) throws Exception{
		return menuDao.getDzSourceByDBIAndShowpkNotIn(dbi, showpkNotIn);
	}

	@Override
	public List<SysDzSource> getDzSourceSome(String dsId) throws Exception {
		return menuDao.getDzSourceSome(dsId);
	}

	@Override
	public boolean delSessionInfo_(String uId) {
		String sql = "delete sys_session_contorl where USERID='" + uId + "'";
		return menuDao.delBySql(sql);
	}



	@Override
	public List loadDzList(String sqlText, String showPk, String showName)
	throws Exception {
		return menuDao.loadDzList(sqlText, showPk, showName);
	}

	@Override
	public SysFunctions getFunctionById(String funcId, String uid)
	throws Exception {
		return menuDao.getFunctionById(funcId, uid);
	}

	@Override
	public Object getLeftMenu(String userId) throws Exception {
		return menuDao.getLeftMenu(userId);
	}

	@Override
	public boolean writeSession(StringBuffer sessionSql, SessionObj sessionObj)
	throws Exception {
		return menuDao.writeSession(sessionSql, sessionObj);
	}

	@Override
	public SessionObj existOperator(String sql, String name, String pass)
	throws Exception {
		return menuDao.existOperator(sql, name, pass);
	}

	@Override
	public List<Map<String, Object>> getAllMenus() {
		return menuDao.getAllMenus();
	}

	@Override
	public List groupByUId(String uid) throws Exception {
		return menuDao.groupByUId(uid);
	}

	@Override
	public List modualByUId(String uid) throws Exception {
		return menuDao.modualByUId(uid);
	}

	@Override
	public List dataStoreAll() throws Exception {
		return this.menuDao.dataStoreAll();
	}

	@Override
	public List getAllGroup() throws Exception {
		return this.menuDao.getAllGroup();
	}

	@Override
	public List getAllDepart() throws Exception {
		return this.menuDao.getAllDepart();
	}

	@Override
	public List dsByModualList(String mId, String oid) throws Exception {
		return this.menuDao.dsByModualList(mId, oid);
	}

	@Override
	public String saveGroupUser(String oid, List<String> gIds, String uid)
	throws Exception {
		return this.menuDao.saveGroupUser(oid, gIds, uid);
	}

	@Override
	public String saveModualUser(String oid, String mids, String uid)
	throws Exception {
		return this.menuDao.saveModualUser(oid, mids, uid);
	}

	@Override
	public String saveDSGN(String mId, String oid,
			List<Map<String, String>> dsList, String uid) throws Exception {
		return this.menuDao.saveDSGN(mId, oid, dsList, uid);
	}

	@Override
	public List<Map<String, Object>> dsByGroMo(String oid, String mId)
	throws Exception {
		return this.menuDao.dsByGroMo(oid, mId);
	}

	@Override
	public boolean executSql(String sql) throws Exception {
		return this.menuDao.executSql(sql);
	}

	@Override
	public ReturnObject sqlByModual(String dsId) throws Exception {
		return this.menuDao.sqlByModual(dsId);
	}

	@Override
	public boolean saveWhere(String dsId, String sql, String oid, String uid)
	throws Exception {
		return this.menuDao.saveWhere(dsId, sql, oid, uid);
	}

	@Override
	public List butnByDS(String dsId, String oid) throws Exception {
		return this.menuDao.butnByDS(dsId, oid);
	}

	@Override
	public List commButn() throws Exception {
		return this.menuDao.commButn();
	}

	@Override
	public String saveDSButn(String dsId, String oid,
			List<Map<String, String>> dsList, String uid) throws Exception {
		return this.menuDao.saveDSButn(dsId, oid, dsList, uid);
	}

	@Override
	public List<Map<String, Object>> butnByGroDs(String oid, String dsId)
	throws Exception {
		return this.menuDao.butnByGroDs(oid, dsId);
	}

	@Override
	public List mgbByGroup(String gid) throws Exception {
		return this.menuDao.mgbByGroup(gid);
	}

	@Override
	public List mgbByGroupQX(String qxgid, List<String> gidList)
	throws Exception {
		return this.menuDao.mgbByGroupQX(qxgid, gidList);
	}

	@Override
	public List gbByUserId(String uid) throws Exception {
		return this.menuDao.gbByUserId(uid);
	}

	@Override
	public List userByGroup(String gId) throws Exception {
		return this.menuDao.userByGroup(gId);
	}



	@Override
	public List getQxzByUid(String uid) {
		//		String sql ="select sr.role_name from sys_role_user sru , sys_role sr   where sru.role_id = sr.role_id and sru.user_id =  '"+uid+"';";
		String sql ="  select *　 from  sys_role sr where sr.role_id in(select sru.role_id from sys_role_user sru where sru.user_id='"+uid+"')";
		return menuDao.getListMap(sql);
	}
	@Override
	public List groupById(String gId) throws Exception {
		return this.menuDao.groupById(gId);
	}

	@Override
	public boolean execGroup(String gId, String gName, String gLevel, String oid)
	throws Exception {
		return this.menuDao.execGroup(gId, gName, gLevel, oid);
	}

	@Override
	public boolean saveGroupPerms(String gId, String allList, String uid)
	throws Exception {
		// String everyFlag = this.menuDao.saveGroupPerms(gId, allList,uid) ;

		boolean flag = this.menuDao.execSaveGroup(gId, allList, uid);
		return flag;
	}

	@Override
	public boolean saveUserPerms(String oid, PermsGroup allList, String uid)
	throws Exception {
		return this.menuDao.saveUserPerms(oid, allList, uid);
	}

	@Override
	public RightList findPerms(String valStr) throws Exception {
		return this.menuDao.findPerms(valStr);
	}

	@Override
	public boolean saveFindUserPerms(String oid, PermsGroup allList, String uid)
	throws Exception {
		return this.menuDao.saveFindUserPerms(oid, allList, uid);
	}

	@Override
	public boolean saveFindGroupPerms(String gid, PermsGroup allList, String uid)
	throws Exception {
		return this.menuDao.saveFindGroupPerms(gid, allList, uid);
	}

	@Override
	public List<Map<String, Object>> querySessionInfo(String key) {
		String sql = "select * from SYS_SESSION_CONTORL t where USERID='" + key+ "'";
		return menuDao.querySessionInfoBySql(sql);
	}

	@Override
	public Boolean saveSessionContorl(SysSessionContorl sessionContorl) {
		return menuDao.saveOrUpdate(sessionContorl);
	}

	@Override
	public boolean delSessionInfo(String uId) {
		String sql = "delete sys_session_contorl where USERID='" + uId + "'";
		return menuDao.delBySql(sql);
	}


	@Override
	public List<SysDzSource> loadDzDataByDsId(String dsId) throws Exception {
		return this.menuDao.loadDzDataByDsId(dsId);
	}

	@Override
	public List<Map<String, Object>> getLoinUserInfo(Object object) {
		String sql = "select o.o_name oname ,o.o_sex,o.o_age ,b.m_name dept from bs_operators o left join bs_man b on o.bumen=b.man_id  where  o_id='"
			+ object + "'";
		return menuDao.getListMap(sql);
	}


	@Override
	public List daoNoHaveCurrentRoleUser(String gId) {
		String sql = "select   bo.o_id,bo.o_name ,bo.bumen from bs_operators bo where  bo.o_exist = '1' and bo.o_id not in (select sru.user_id  from sys_role_user sru where   "
			+ "sru.role_id= '" + gId + "' )";
		return menuDao.getUser(sql);
	}

	@Override
	public boolean SveInserROleGoopUser(String gId, String insertID,
			String delID) {
		String[] insterid = insertID.split(";");
		String inserSql = null;
		if (delID != "" && delID != null) {
			String delSql = "delete sys_role_user s where s.user_id in ("
				+ delID.substring(0, delID.length() - 1) + ") and  s.ROLE_ID='"+gId+"' ";
			if (!menuDao.executSql(delSql)) {
				return false;
			}
		}

		for (int i = 0; i < insterid.length; i++) {
			if (insterid[i] != null && insterid[i] != "") {
				inserSql = "insert into sys_role_user(user_id,role_id) values('"
					+ insterid[i] + "','" + gId + "')";
				if (!menuDao.executSql(inserSql)) {
					return false;
				}
			}
		}
		return true;
	}

	@Override
	public List getALLbumen() {
		String sql = "select BM.MAN_ID,BM.M_NAME from bs_man bm where bm.man_type = 'bumen'";
		return menuDao.querySessionInfoBySql(sql);
	}

	@Override
	public RightList SvequeryInfo(RightList riList) {

		StringBuffer haveSqlString = new StringBuffer("select  bo.o_id,bo.o_name,bo.bumen  from bs_operators bo where O_EXIST = 1 and bo.o_id  in (select sru.user_id  from sys_role_user sru where sru.role_id='"+riList.getGid()+"')  ");
		if(riList!=null&&riList.getOname()!=null&&riList.getOname()!=""){
			haveSqlString.append("and bo.o_name like '%"+riList.getOname()+"%'");
		}

		if(riList!=null&&riList.getBumenid()!=null&&riList.getBumenid()!=""){
			haveSqlString.append("and bo.bumen='"+riList.getBumenid()+"'");
		}

		StringBuffer noHaveSql = new StringBuffer("select   bo.o_id,bo.o_name ,bo.bumen from bs_operators bo where O_EXIST = 1 and bo.o_id not in (select sru.user_id  from sys_role_user sru where sru.role_id= '" + riList.getGid() + "')  ");
		if(riList!=null&&riList.getOname()!=null&&riList.getOname()!=""){
			noHaveSql.append("and bo.o_name like '%"+riList.getOname()+"%'");
		}
		if(riList!=null&&riList.getBumenid()!=null&&riList.getBumenid()!=""){
			noHaveSql.append("and bo.bumen='"+riList.getBumenid()+"'");
		}

		List haveJurisdiction = menuDao.querySessionInfoBySql(haveSqlString.toString());
		List noHaveJurisdiction = menuDao.querySessionInfoBySql(noHaveSql.toString());
		RightList rightList = new RightList();
		rightList.setHaveJurisdiction(haveJurisdiction);
		rightList.setNoHaveJurisdiction(noHaveJurisdiction);

		return rightList;
	}

	@Override
	public Object getSubSysRight(String userId, String menuId) throws Exception {
		return menuDao.getSubSysRight(userId, menuId);
	}

}
